#!/usr/bin/env python3
"""
SERP (搜索引擎结果页面) 处理模块
用于生成Google搜索查询语句，根据行业分类搜索特定网站内容
"""

import re
from typing import Dict, List, Optional, Tuple
from urllib.parse import urlparse, urlunparse
import logging

logger = logging.getLogger(__name__)

# 行业分类常量字典
INDUSTRY_CATEGORIES = {
    "Business & Finance": [
        "Business", "Startups", "Finance", "E-commerce", 
        "Marketing and Advertising", "Manufacturing", "Real Estate"
    ],
    
    "Technology & Computing": [
        "Technology", "Programming", "Hardware development", 
        "Software development", "Web-development", "Computers", 
        "Mobile", "Internet", "Gadgets"
    ],
    
    "Health & Lifestyle": [
        "Health", "Beauty", "Food", "Lifestyle", "Fashion", "For Men"
    ],
    
    "Education & Learning": [
        "Education", "Books", "Literature", "Science", "For Children"
    ],
    
    "Entertainment & Leisure": [
        "Entertainment", "Movies", "Music", "Games", "Sports", 
        "Leisure and Hobbies", "Photography"
    ],
    
    "Environment & Sustainability": [
        "Environment", "Nature", "Animals and Pets", "Agriculture"
    ],
    
    "Society & Culture": [
        "Society", "Culture", "Politics", "Law", "Public Service", 
        "News and Media", "Art", "Personal Blogs", "Miscellaneous"
    ],
    
    "Career & Employment": [
        "Career and Employment"
    ],
    
    "Home & Living": [
        "Home and Family", "Construction and Repairs", "Shopping", 
        "Equipment", "Real Estate"
    ],
    
    "Transport & Travel": [
        "Transport", "Automobiles", "Travelling", "Places"
    ]
}

# 行业关键词映射（用于生成更具体的搜索词）
INDUSTRY_KEYWORDS_MAP = {
    "Business & Finance": [
        "business strategy", "startup funding", "financial planning", "e-commerce trends",
        "digital marketing", "manufacturing process", "real estate investment",
        "market analysis", "business growth", "entrepreneurship"
    ],
    
    "Technology & Computing": [
        "artificial intelligence", "machine learning", "software development", 
        "web development", "mobile apps", "cloud computing", "cybersecurity",
        "programming languages", "tech innovation", "gadget reviews"
    ],
    
    "Health & Lifestyle": [
        "health tips", "beauty products", "healthy recipes", "lifestyle trends",
        "fashion style", "men's grooming", "wellness", "skincare",
        "fitness", "nutrition"
    ],
    
    "Education & Learning": [
        "online learning", "educational technology", "scientific research",
        "children's education", "book reviews", "literature analysis",
        "STEM education", "learning methods"
    ],
    
    "Entertainment & Leisure": [
        "movie reviews", "music streaming", "gaming news", "sports updates",
        "hobby ideas", "photography tips", "entertainment news",
        "leisure activities", "concert reviews"
    ],
    
    "Environment & Sustainability": [
        "environmental protection", "climate change", "sustainable living",
        "wildlife conservation", "organic farming", "renewable energy",
        "eco-friendly products", "green technology"
    ],
    
    "Society & Culture": [
        "social issues", "cultural events", "political news", "legal advice",
        "public policy", "news analysis", "art exhibitions", "blog posts",
        "community events", "social trends"
    ],
    
    "Career & Employment": [
        "job search", "career development", "workplace skills", "resume tips",
        "interview preparation", "professional growth", "employment trends",
        "remote work", "salary negotiation"
    ],
    
    "Home & Living": [
        "home improvement", "family activities", "DIY projects", "home decor",
        "kitchen gadgets", "furniture", "household products", "home security",
        "gardening", "cleaning tips"
    ],
    
    "Transport & Travel": [
        "travel destinations", "transportation news", "automotive reviews",
        "travel tips", "car maintenance", "public transport", "flight deals",
        "road trips", "travel planning"
    ]
}

class SERPQueryGenerator:
    """SERP查询语句生成器"""
    
    def __init__(self):
        self.industry_categories = INDUSTRY_CATEGORIES
        self.industry_keywords = INDUSTRY_KEYWORDS_MAP
    
    def clean_url(self, url: str) -> str:
        """
        清理和标准化URL
        
        Args:
            url: 输入的URL
            
        Returns:
            str: 清理后的URL
        """
        if not url:
            raise ValueError("URL不能为空")
        
        # 如果URL没有协议，添加https://
        if not url.startswith(('http://', 'https://')):
            url = 'https://' + url
        
        try:
            parsed = urlparse(url)
            
            # 确保域名有效
            if not parsed.netloc:
                raise ValueError(f"无效的URL: {url}")
            
            # 重构URL，只保留协议和域名
            clean_url = urlunparse((
                parsed.scheme or 'https',
                parsed.netloc,
                '',  # path
                '',  # params
                '',  # query
                ''   # fragment
            ))
            
            return clean_url
            
        except Exception as e:
            raise ValueError(f"URL解析失败: {url}, 错误: {e}")
    
    def generate_category_query(self, url: str, category: str) -> str:
        """
        为特定行业类别生成Google搜索查询
        
        Args:
            url: 目标网站URL
            category: 行业类别名称
            
        Returns:
            str: Google搜索查询语句
        """
        if category not in self.industry_categories:
            raise ValueError(f"不支持的行业类别: {category}")
        
        clean_url = self.clean_url(url)
        subcategories = self.industry_categories[category]
        
        # 构建OR查询语句
        or_terms = []
        for subcategory in subcategories:
            or_terms.append(f'"{subcategory}"')
        
        query = f'site:{clean_url} ({" OR ".join(or_terms)})'
        
        logger.info(f"生成查询语句 - 类别: {category}, URL: {clean_url}")
        logger.debug(f"查询语句: {query}")
        
        return query
    
    def generate_keyword_query(self, url: str, category: str) -> str:
        """
        为特定行业类别生成基于关键词的Google搜索查询
        
        Args:
            url: 目标网站URL
            category: 行业类别名称
            
        Returns:
            str: 基于关键词的Google搜索查询语句
        """
        if category not in self.industry_keywords:
            raise ValueError(f"不支持的行业类别: {category}")
        
        clean_url = self.clean_url(url)
        keywords = self.industry_keywords[category]
        
        # 构建OR查询语句
        or_terms = []
        for keyword in keywords:
            or_terms.append(f'"{keyword}"')
        
        query = f'site:{clean_url} ({" OR ".join(or_terms)})'
        
        logger.info(f"生成关键词查询语句 - 类别: {category}, URL: {clean_url}")
        logger.debug(f"查询语句: {query}")
        
        return query
    
    def generate_all_category_queries(self, url: str) -> Dict[str, str]:
        """
        为所有行业类别生成Google搜索查询
        
        Args:
            url: 目标网站URL
            
        Returns:
            Dict[str, str]: 行业类别到查询语句的映射
        """
        clean_url = self.clean_url(url)
        queries = {}
        
        for category in self.industry_categories:
            try:
                queries[category] = self.generate_category_query(clean_url, category)
            except Exception as e:
                logger.error(f"生成查询失败 - 类别: {category}, 错误: {e}")
                continue
        
        logger.info(f"为URL {clean_url} 生成了 {len(queries)} 个查询语句")
        return queries
    
    def generate_all_keyword_queries(self, url: str) -> Dict[str, str]:
        """
        为所有行业类别生成基于关键词的Google搜索查询
        
        Args:
            url: 目标网站URL
            
        Returns:
            Dict[str, str]: 行业类别到关键词查询语句的映射
        """
        clean_url = self.clean_url(url)
        queries = {}
        
        for category in self.industry_keywords:
            try:
                queries[category] = self.generate_keyword_query(clean_url, category)
            except Exception as e:
                logger.error(f"生成关键词查询失败 - 类别: {category}, 错误: {e}")
                continue
        
        logger.info(f"为URL {clean_url} 生成了 {len(queries)} 个关键词查询语句")
        return queries
    
    def get_category_info(self, category: str) -> Dict[str, List[str]]:
        """
        获取特定行业类别的详细信息
        
        Args:
            category: 行业类别名称
            
        Returns:
            Dict[str, List[str]]: 包含子分类和关键词的信息
        """
        if category not in self.industry_categories:
            raise ValueError(f"不支持的行业类别: {category}")
        
        return {
            "subcategories": self.industry_categories[category],
            "keywords": self.industry_keywords.get(category, [])
        }
    
    def list_all_categories(self) -> List[str]:
        """
        获取所有支持的行业类别列表
        
        Returns:
            List[str]: 行业类别列表
        """
        return list(self.industry_categories.keys())
    
    def search_categories_by_keyword(self, keyword: str) -> List[str]:
        """
        根据关键词搜索相关的行业类别
        
        Args:
            keyword: 搜索关键词
            
        Returns:
            List[str]: 匹配的行业类别列表
        """
        keyword_lower = keyword.lower()
        matching_categories = []
        
        for category, subcategories in self.industry_categories.items():
            # 检查类别名称
            if keyword_lower in category.lower():
                matching_categories.append(category)
                continue
            
            # 检查子分类
            for subcategory in subcategories:
                if keyword_lower in subcategory.lower():
                    matching_categories.append(category)
                    break
            
            # 检查关键词
            keywords = self.industry_keywords.get(category, [])
            for kw in keywords:
                if keyword_lower in kw.lower():
                    matching_categories.append(category)
                    break
        
        return list(set(matching_categories))  # 去重

def create_google_search_url(query: str) -> str:
    """
    将查询语句转换为Google搜索URL
    
    Args:
        query: Google搜索查询语句
        
    Returns:
        str: Google搜索URL
    """
    import urllib.parse
    encoded_query = urllib.parse.quote_plus(query)
    return f"https://www.google.com/search?q={encoded_query}"

# 全局查询生成器实例
serp_generator = SERPQueryGenerator()

# 便捷函数
def generate_site_query(url: str, category: str) -> str:
    """
    便捷函数：生成站点搜索查询
    
    Args:
        url: 目标网站URL
        category: 行业类别
        
    Returns:
        str: Google搜索查询语句
    """
    return serp_generator.generate_category_query(url, category)

def generate_all_queries(url: str) -> Dict[str, str]:
    """
    便捷函数：生成所有类别的搜索查询
    
    Args:
        url: 目标网站URL
        
    Returns:
        Dict[str, str]: 所有查询语句
    """
    return serp_generator.generate_all_category_queries(url)

def get_supported_categories() -> List[str]:
    """
    便捷函数：获取支持的行业类别
    
    Returns:
        List[str]: 行业类别列表
    """
    return serp_generator.list_all_categories()

if __name__ == "__main__":
    print(get_supported_categories())